package filters;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by hadoop on 17-6-14.
 * Filter类：用于日志功能
 */
@WebFilter(filterName = "LogFilter",urlPatterns = {"/*"},asyncSupported = true)
public class LogFilter implements Filter{
    private FilterConfig config;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.config = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
//        ServletContext application = this.config.getServletContext();
        //用户请求被执行前的处理
        long before =System.currentTimeMillis();
        System.out.println("开始过滤...");
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        System.out.println("Filter拦截到用户请求地址: "+request.getServletPath());
        filterChain.doFilter(servletRequest,servletResponse);
        //服务器响应后的处理
        long after = System.currentTimeMillis();
        System.out.println("过滤结束");
        System.out.println("请求被定位到: "+request.getRequestURI()+"用时: "+(after-before)+" ms");
    }

    @Override
    public void destroy() {
        this.config = null;
    }
}
